﻿@model PromoCodes_GUI.Models.PromotionViewModel

@{
    ViewBag.Title = "Add Promotion";
}
<h2>Add Promotion</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
    function TogglePromotionType(promotionType) {
        var product = $("#ProductID");
        var order = $("#Promotion_MinimumTotalOrder");
        if (promotionType == 0) {
            product.attr("disabled", false);
            order.attr("disabled", true);
        }
        else {
            product.attr("disabled", true);
            order.attr("disabled", false);
        }
    }
</script>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.Promotion.PromotionID)
        <div class="editor-prop">
            <div class="editor-label">
                @Html.LabelFor(model => model.Promotion.PromotionCode, "Promotion Code:")
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Promotion.PromotionCode)
                @Html.ValidationMessageFor(model => model.Promotion.PromotionCode)
            </div>
        </div>
        <div class="editor-prop">
            <div class="editor-label">
                @Html.LabelFor(model => model.Promotion.DiscountPercentage, "Discount Percentage:")
            </div>
            <div class="editor-field">
                <input type="number" name="Promotion.DiscountPercentage" id="Promotion.DiscountPercentage" value="@Model.Promotion.DiscountPercentage"  />
                @Html.ValidationMessageFor(model => model.Promotion.DiscountPercentage)
            </div>
        </div>
        
        <div class="editor-prop">
            <div class="editor-label">
                @Html.LabelFor(model => model.Promotion.Expiration, "Expiration:")
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Promotion.Expiration, new { @class = "date" })
                @Html.ValidationMessageFor(model => model.Promotion.Expiration)
            </div>
        </div>
        
        <div class="editor-prop">
            <div class="editor-label">
                @Html.LabelFor(model => model.Promotion.MaxUses, "Maximum Uses:")
            </div>
            <div class="editor-field">
                <input type="number" name="Promotion.MaxUses" id="Promotion.MaxUses" value="@Model.Promotion.MaxUses"/>
                @Html.ValidationMessageFor(model => model.Promotion.MaxUses)
            </div>
        </div>
        
        <div class="editor-prop">
            <div class="editor-label">
                @Html.LabelFor(model => model.Promotion.DiscountType, "Discount Type:")
            </div>
            <div class="editor-field">
                <div id="OrderDiscount">
                    @Html.RadioButtonFor(model => model.Promotion.DiscountType, (int)PromoCodes_Models.Entities.DiscountType.ORDER, (Model.Promotion.discountTypeValue == (int)PromoCodes_Models.Entities.DiscountType.ORDER) ?
                                                                                (object) new { onclick = "TogglePromotionType(" + (int)PromoCodes_Models.Entities.DiscountType.ORDER + ")" , @checked = "checked" } :
                                                                              new { onclick = "TogglePromotionType(" + (int)PromoCodes_Models.Entities.DiscountType.ORDER + ")" }) Order
                        Minimum:  
                        @Html.EditorFor(x => x.Promotion.MinimumTotalOrder, (Model.Promotion.discountTypeValue == (int)PromoCodes_Models.Entities.DiscountType.PRODUCT) ?
                                                                            (object)new { disabled="disabled" } :
                                                                            new { })
                        @Html.ValidationMessageFor(model => model.Promotion.MinimumTotalOrder)
                </div>
                <div id="ProductDiscount">
                    @Html.RadioButtonFor(model => model.Promotion.DiscountType, (int)PromoCodes_Models.Entities.DiscountType.PRODUCT,
                        (Model.Promotion.DiscountType == PromoCodes_Models.Entities.DiscountType.PRODUCT) ?
                        (object) new { onclick = "TogglePromotionType(" + (int)PromoCodes_Models.Entities.DiscountType.PRODUCT + ")" , @checked = "checked" } :
                        new { onclick = "TogglePromotionType(" + (int)PromoCodes_Models.Entities.DiscountType.PRODUCT + ")" } ) Product
                    @Html.DropDownListFor(model => model.ProductID, Model.AllProducts, (Model.Promotion.DiscountType == PromoCodes_Models.Entities.DiscountType.ORDER) ?
                        (object)new { disabled="disabled" } :
                        new { } 
                     )
                </div>
                @Html.ValidationMessageFor(model => model.AllProducts)
            </div>
        </div>
        
        <div class="editor-prop">
            <div class="editor-label">
                @Html.LabelFor(model => model.Promotion.Notes)
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model.Promotion.Notes)
                @Html.ValidationMessageFor(model => model.Promotion.Notes)
            </div>
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
